package com.foo.common.base.utils;

import java.io.IOException;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;

import com.google.common.base.Charsets;
import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class FooUtils {

	public final static int ddl_pk_length = 36;// uuid2模式下，36是标准的id长度
	public final static String session_user_id = "uid";

	public static void printJsonFromMap(HttpServletResponse response,
			Map<String, Object> myResultMap) {
		try {
			Gson gson = new GsonBuilder().disableHtmlEscaping()
					.serializeNulls().create();
			response.setCharacterEncoding(Charsets.UTF_8.toString());
			response.setContentType("text/plain");
			response.getWriter().write(gson.toJson(myResultMap));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void printSuccessJson(HttpServletResponse response) {
		Map<String, Object> myResultMap = Maps.newHashMap();
		myResultMap.put("ret", 0);
		myResultMap.put("msg", "");
		printJsonFromMap(response, myResultMap);
	}

	public static void printSuccessJson(HttpServletResponse response,
			Object myObject) {
		Map<String, Object> myResultMap = Maps.newHashMap();
		myResultMap.put("ret", 0);
		myResultMap.put("msg", myObject);
		printJsonFromMap(response, myResultMap);
	}

	public static String generateUUID() {
		return UUID.randomUUID().toString();
	}

}
